package benny_driving_ser.util;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class FormatUtil {
	public static String getTimeFormatNow() {
		return getTimeFormat(System.currentTimeMillis());
	}

	public static String getTimeFormat(long millis) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(millis);
		String time = c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1)
				+ "-" + c.get(Calendar.DAY_OF_MONTH) + " "
				+ c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE)
				+ ":" + c.get(Calendar.SECOND);
		return time;
	}

	public static double gps2m(double lat_a, double lng_a, double lat_b,
			double lng_b) {
		double earth_radius = 6378137.0;
		double radLat1 = (lat_a * Math.PI / 180.0);
		double radLat2 = (lat_b * Math.PI / 180.0);
		double a = radLat1 - radLat2;
		double b = (lng_a - lng_b) * Math.PI / 180.0;
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(radLat1) * Math.cos(radLat2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * earth_radius;
		s = Math.round(s * 10000) / 10000;
		return s;
	}

	public static double getFormatDouble(int decimalPoint, double params) {
		if (decimalPoint < 1) {
			return Math.floor(params);
		}
		StringBuffer sb = new StringBuffer(".");
		for (int i = 0; i < decimalPoint; i++) {
			sb.append("#");
		}
		DecimalFormat df = new DecimalFormat(sb.toString());
		return Double.parseDouble(df.format(params));
	}
	
	public static String getDateTimeNow() {
		return millis2datetime(System.currentTimeMillis());
	}

	public static String millis2datetime(long millis) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(millis);
		String time = c.get(Calendar.YEAR) + "-" + 
				((c.get(Calendar.MONTH) + 1)>9?(c.get(Calendar.MONTH) + 1):"0"+(c.get(Calendar.MONTH) + 1))
				+ "-" + (c.get(Calendar.DAY_OF_MONTH)>9?
						c.get(Calendar.DAY_OF_MONTH):"0"+c.get(Calendar.DAY_OF_MONTH)) + " "
				+ (c.get(Calendar.HOUR_OF_DAY)>9?
						c.get(Calendar.HOUR_OF_DAY):"0"+c.get(Calendar.HOUR_OF_DAY)) + ":" + (c.get(Calendar.MINUTE)>9?c.get(Calendar.MINUTE):"0"+c.get(Calendar.MINUTE))
				+ ":" + (c.get(Calendar.SECOND)>9?c.get(Calendar.SECOND):"0"+c.get(Calendar.SECOND));
		return time;
	}
	
	/**
	 * 自然月计算
	 * @param months
	 * @return
	 */
	public static String calcMonth(int months){
		String str = ""; 
		str = getDateTimeNow();
		System.out.println(str);
		String year = str.split(" ")[0].split("-")[0];
		String month = str.split(" ")[0].split("-")[1];
		String day = str.split(" ")[0].split("-")[2];
		if((Integer.parseInt(month)+months)>12){
			str = Integer.parseInt(str.split(" ")[0].split("-")[0])+(months+Integer.parseInt(month))/12
				+"-"+((months+Integer.parseInt(month))%12>9?(months+Integer.parseInt(month))%12:"0"+(months+Integer.parseInt(month))%12)
				+"-"+day+" "+str.split(" ")[1];
			System.out.println(str);
		}else{
			str = year+"-"+(Integer.parseInt(month)+months>9?(Integer.parseInt(month)+months):"0"+(Integer.parseInt(month)+months))+"-"+day+" "+str.split(" ")[1];
			System.out.println(str);
		}
		return str;
	}
	
	public static long dateString2long(String datetime){
		
		Calendar c = Calendar.getInstance();
		  
		try {
			c.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(datetime));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return c.getTimeInMillis();
	}
	
//	public static void main(String[] args) {
//		calcMonth(100+2400);
//	}
}
